#!/bin/bash

if [ $# -ne "1" ]
then
    echo "USO: ${0} <CLIENT_NAME>"
    exit 0
fi

CLIENT_NAME=${1}

#Redes y mascaras para el ruteo
TOYOTA="10.11.22.0"
AUDI="10.11.23.0"
FORD="192.168.53.0"
FIAT="10.134.1.0"
PEUGEOT="10.134.13.0"
LAMBORGHINI="10.134.13.128"
ROLLSROYCE="10.134.13.160"
BENTLEY="10.134.13.192"
VOLKSWAGEN="10.134.13.224"
SUZUKI="10.9.12.192"
FERRARI="10.134.5.128"
CADILLAC="10.134.5.160"
MERCEDEZBENZ="10.134.5.164"
RENAULT="137.43.1.0" #INTERNET
FRAMERELAY="172.3.1.192"
TUNEL="10.134.13.240"

#MASCARAS
MASK_1="255.255.255.0"
MASK_2="255.255.255.128"
MASK_3="255.255.255.192"
MASK_4="255.255.255.224"
MASK_5="255.255.255.240"
MASK_6="255.255.255.252"

IP_TELSERVER1="10.134.1.130"
BROADCAST_TELSERVER1="10.134.1.255"
MASCARA_TELSERVER1=$MASK_1
IP_TELSERVER2="10.134.13.111"
BROADCAST_TELSERVER2="10.134.13.127"
MASCARA_TELSERVER2=$MASK_2

source config.sh  # incluyo las constantes

#Borra el gateway existente
sudo ufw disable
sudo route del default
if [ "${CLIENT_NAME}" == "HOSTA" ]
then
	TAP=${HOSTA[0]}

	sudo route add -net "${TOYOTA}" netmask "${MASK_1}" gw "10.11.23.1" dev "${TAP}"
	sudo route add -net "${FORD}" netmask "${MASK_1}" gw "10.11.23.6" dev "${TAP}"
	sudo route add -net "${FIAT}" netmask "${MASK_1}" gw "10.11.23.3" dev "${TAP}"
	sudo route add -net "${PEUGEOT}" netmask "${MASK_2}" gw "10.11.23.6" dev "${TAP}"
	sudo route add -net "${LAMBORGHINI}" netmask "${MASK_4}" gw "10.11.23.3" dev "${TAP}"
	sudo route add -net "${ROLLSROYCE}" netmask "${MASK_4}" gw "10.11.23.3" dev "${TAP}"
	sudo route add -net "${BENTLEY}" netmask "${MASK_4}" gw "10.11.23.1" dev "${TAP}"
	sudo route add -net "${VOLKSWAGEN}" netmask "${MASK_5}" gw "10.11.23.1" dev "${TAP}"
	sudo route add -net "${SUZUKI}" netmask "${MASK_3}" gw "10.11.23.1" dev "${TAP}"
	sudo route add -net "${FERRARI}" netmask "${MASK_4}" gw "10.11.23.2" dev "${TAP}"
	sudo route add -net "${CADILLAC}" netmask "${MASK_6}" gw "10.11.23.3" dev "${TAP}"
	sudo route add -net "${MERCEDEZBENZ}" netmask "${MASK_6}" gw "10.11.23.1" dev "${TAP}"
	sudo route add -net "${FRAMERELAY}" netmask "${MASK_3}" gw "10.11.23.1" dev "${TAP}"
	sudo route add -net "${RENAULT}" netmask "${MASK_1}" gw "10.11.23.6" dev "${TAP}"
	sudo route add -net "${TUNEL}" netmask "${MASK_5}" gw "10.11.23.6" dev "${TAP}"
	
elif [ "${CLIENT_NAME}" == "HOSTB" ]
then
	TAP=${HOSTB[0]}

	sudo route add -net "${TOYOTA}" netmask "${MASK_1}" gw "10.134.1.3" dev "${TAP}"
	sudo route add -net "${AUDI}" netmask "${MASK_1}" gw "10.134.1.1" dev "${TAP}"
	sudo route add -net "${FORD}" netmask "${MASK_1}" gw "10.134.1.1" dev "${TAP}"
	sudo route add -net "${PEUGEOT}" netmask "${MASK_2}" gw "10.134.1.3" dev "${TAP}"
	sudo route add -net "${LAMBORGHINI}" netmask "${MASK_4}" gw "10.134.1.3" dev "${TAP}"
	sudo route add -net "${ROLLSROYCE}" netmask "${MASK_4}" gw "10.134.1.4" dev "${TAP}"
	sudo route add -net "${BENTLEY}" netmask "${MASK_4}" gw "10.134.1.3" dev "${TAP}"
	sudo route add -net "${VOLKSWAGEN}" netmask "${MASK_5}" gw "10.134.1.1" dev "${TAP}"
	sudo route add -net "${SUZUKI}" netmask "${MASK_3}" gw "10.134.1.3" dev "${TAP}"
	sudo route add -net "${FERRARI}" netmask "${MASK_4}" gw "10.134.1.1" dev "${TAP}"
	sudo route add -net "${CADILLAC}" netmask "${MASK_6}" gw "10.134.1.1" dev "${TAP}"
	sudo route add -net "${MERCEDEZBENZ}" netmask "${MASK_6}" gw "10.134.1.3" dev "${TAP}"
	sudo route add -net "${FRAMERELAY}" netmask "${MASK_3}" gw "10.134.1.1" dev "${TAP}"
	sudo route add -net "${RENAULT}" netmask "${MASK_1}" gw "10.134.1.3" dev "${TAP}"
	sudo route add -net "${TUNEL}" netmask "${MASK_5}" gw "10.134.1.3" dev "${TAP}"


elif [ "${CLIENT_NAME}" == "HOSTC" ]
then
	TAP=${HOSTC[0]}

	sudo route add -net "${TOYOTA}" netmask "${MASK_1}" gw "10.9.12.194" dev "${TAP}" #R14
	sudo route add -net "${AUDI}" netmask "${MASK_1}" gw "10.9.12.193" dev "${TAP}"  #R13
	sudo route add -net "${FORD}" netmask "${MASK_1}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${FIAT}" netmask "${MASK_1}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${PEUGEOT}" netmask "${MASK_2}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${LAMBORGHINI}" netmask "${MASK_4}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${ROLLSROYCE}" netmask "${MASK_4}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${BENTLEY}" netmask "${MASK_4}" gw "10.9.12.195" dev "${TAP}" #R15
	sudo route add -net "${VOLKSWAGEN}" netmask "${MASK_5}" gw "10.9.12.194" dev "${TAP}" #R14
	sudo route add -net "${FERRARI}" netmask "${MASK_4}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${CADILLAC}" netmask "${MASK_6}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${MERCEDEZBENZ}" netmask "${MASK_6}" gw "10.9.12.193" dev "${TAP}" #R13
	sudo route add -net "${FRAMERELAY}" netmask "${MASK_3}" gw "10.9.12.193" dev "${TAP}"
	sudo route add -net "${RENAULT}" netmask "${MASK_1}" gw "10.9.12.195" dev "${TAP}"
	sudo route add -net "${TUNEL}" netmask "${MASK_5}" gw "10.9.12.195" dev "${TAP}"
	
elif [ "${CLIENT_NAME}" == "WEBSERVER" ]
then
    TAP=${WEBSERVER[0]}

	sudo route add -net "${TOYOTA}" netmask "${MASK_1}" gw "192.168.53.5" dev "${TAP}"
	sudo route add -net "${AUDI}" netmask "${MASK_1}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${FIAT}" netmask "${MASK_1}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${PEUGEOT}" netmask "${MASK_2}" gw "192.168.53.5" dev "${TAP}"
	sudo route add -net "${LAMBORGHINI}" netmask "${MASK_4}" gw "192.168.53.5" dev "${TAP}"
	sudo route add -net "${ROLLSROYCE}" netmask "${MASK_4}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${BENTLEY}" netmask "${MASK_4}" gw "192.168.53.5" dev "${TAP}"
	sudo route add -net "${VOLKSWAGEN}" netmask "${MASK_5}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${SUZUKI}" netmask "${MASK_3}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${FERRARI}" netmask "${MASK_4}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${CADILLAC}" netmask "${MASK_6}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${MERCEDEZBENZ}" netmask "${MASK_6}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${FRAMERELAY}" netmask "${MASK_3}" gw "192.168.53.4" dev "${TAP}"
	sudo route add -net "${RENAULT}" netmask "${MASK_1}" gw "192.168.53.5" dev "${TAP}"
	sudo route add -net "${TUNEL}" netmask "${MASK_5}" gw "192.168.53.5" dev "${TAP}"
	
elif [ "${CLIENT_NAME}" == "FTPSERVER" ]
then
    TAP=${FTPSERVER[0]}
    
	sudo route add -net "${AUDI}" netmask "${MASK_1}" gw "10.11.22.2" dev "${TAP}" #R14
	sudo route add -net "${FORD}" netmask "${MASK_1}" gw "10.11.22.4" dev "${TAP}" #R16
	sudo route add -net "${FIAT}" netmask "${MASK_1}" gw "10.11.22.3" dev "${TAP}" #R14
	sudo route add -net "${PEUGEOT}" netmask "${MASK_2}" gw "10.11.22.2" dev "${TAP}" #R14
	sudo route add -net "${LAMBORGHINI}" netmask "${MASK_4}" gw "10.11.22.3" dev "${TAP}" #R14
	sudo route add -net "${ROLLSROYCE}" netmask "${MASK_4}" gw "10.11.22.2" dev "${TAP}" #R14
	sudo route add -net "${BENTLEY}" netmask "${MASK_4}" gw "10.11.22.4" dev "${TAP}" #R16
	sudo route add -net "${VOLKSWAGEN}" netmask "${MASK_5}" gw "10.11.22.3" dev "${TAP}" #R14
	sudo route add -net "${SUZUKI}" netmask "${MASK_3}" gw "10.11.22.2" dev "${TAP}" #R14
	sudo route add -net "${FERRARI}" netmask "${MASK_4}" gw "10.11.22.3" dev "${TAP}" #R14
	sudo route add -net "${CADILLAC}" netmask "${MASK_6}" gw "10.11.22.2" dev "${TAP}" #R14
	sudo route add -net "${MERCEDEZBENZ}" netmask "${MASK_6}" gw "10.11.22.3" dev "${TAP}" #R14
	sudo route add -net "${FRAMERELAY}" netmask "${MASK_3}" gw "10.11.22.2" dev "${TAP}"
	sudo route add -net "${RENAULT}" netmask "${MASK_1}" gw "10.11.22.4" dev "${TAP}"
	sudo route add -net "${TUNEL}" netmask "${MASK_5}" gw "10.11.22.4" dev "${TAP}"
	
elif [ "${CLIENT_NAME}" == "TELSERVER" ]
then
    TAP1=${TELSERVER1[0]} #FIAT
    TAP2=${TELSERVER2[0]} #PEUGEOT
    
    #THOSE WHO ENTER HERE LOSE ALL HOPE
    
    sudo ifconfig $TAP1 $IP_TELSERVER1 broadcast $BROADCAST_TELSERVER1 netmask $MASCARA_TELSERVER1
	sudo ifconfig $TAP2 $IP_TELSERVER2 broadcast $BROADCAST_TELSERVER2 netmask $MASCARA_TELSERVER2

	#Configuracion del enrutamiento estatico
	sudo echo 3 table1 >> /etc/iproute2/rt_tables
	sudo echo 2 table2 >> /etc/iproute2/rt_tables
	sudo ip route add $TOYOTA/24 via 10.134.1.3 table table1
	sudo ip route add $TOYOTA/24 via 10.134.13.1 table table2
	sudo ip route add $FORD/24 via 10.134.1.1 table table1
	sudo ip route add $FORD/24 via 10.134.13.3 table table2
	sudo ip route add $AUDI/24 via 10.134.1.1 table table1
	sudo ip route add $AUDI/24 via 10.134.13.1 table table2
	sudo ip route add $LAMBORGHINI/27 via 10.134.1.3 table table1
	sudo ip route add $LAMBORGHINI/27 via 10.134.13.2 table table2
	sudo ip route add $ROLLSROYCE/27 via 10.134.1.4 table table1
	sudo ip route add $ROLLSROYCE/27 via 10.134.13.1 table table2
	sudo ip route add $BENTLEY/27 via 10.134.1.3 table table1
	sudo ip route add $BENTLEY/27 via 10.134.13.3 table table2
	sudo ip route add $VOLKSWAGEN/28 via 10.134.1.3 table table1
	sudo ip route add $VOLKSWAGEN/28 via 10.134.13.1 table table2
	sudo ip route add $SUZUKI/26 via 10.134.1.3 table table1
	sudo ip route add $SUZUKI/26 via 10.134.13.1 table table2
	sudo ip route add $FERRARI/27 via 10.134.1.1 table table1
	sudo ip route add $FERRARI/27 via 10.134.13.1 table table2
	sudo ip route add $CADILLAC/30 via 10.134.1.1 table table1
	sudo ip route add $CADILLAC/30 via 10.134.13.1 table table2
	sudo ip route add $MERCEDEZBENZ/30 via 10.134.1.3 table table1
	sudo ip route add $MERCEDEZBENZ/30 via 10.134.13.1 table table2
	sudo ip route add $FRAMERELAY/26 via 10.134.1.1 table table1
	sudo ip route add $FRAMERELAY/26 via 10.134.13.1 table table2
	sudo ip route add $RENAULT/24 via 10.134.1.3 table table1
	sudo ip route add $RENAULT/24 via 10.134.13.3 table table2
	sudo ip route add $TUNEL/28 via 10.134.1.3 table table1
	sudo ip route add $TUNEL/28 via 10.134.13.3 table table2
	sudo ip route add $PEUGEOT/25 via 10.134.1.3 table table1
	sudo ip route add $PEUGEOT/25 dev $TAP2 table table2
	sudo ip route add $FIAT/24 dev $TAP1 table table1
	sudo ip route add $FIAT/24 via 10.134.13.1 table table2

	sudo ip rule flush

	sudo ip rule add table main prio 32766
	sudo ip rule add table default prio 32767
	sudo ip rule add from $IP_TELSERVER2 lookup table2 prio 1001
	sudo ip rule add to $IP_TELSERVER2 lookup table2 prio 1002
	sudo ip rule add from $IP_TELSERVER1 lookup table1 prio 1003
	sudo ip rule add to $IP_TELSERVER1 lookup table1 prio 1004
	sudo ip rule add table table1 prio 1101
	sudo ip rule add table table2 prio 1102    
    
    
    
    #route TAP1   
 	#sudo route add -net "${TOYOTA}" netmask "${MASK_1}" gw "10.134.1.3" dev "${TAP1}"
	#sudo route add -net "${AUDI}" netmask "${MASK_1}" gw "10.134.1.1" dev "${TAP1}"
	#sudo route add -net "${FORD}" netmask "${MASK_1}" gw "10.134.1.1" dev "${TAP1}"
	#sudo route add -net "${LAMBORGHINI}" netmask "${MASK_4}" gw "10.134.1.3" dev "${TAP1}"
	#sudo route add -net "${ROLLSROYCE}" netmask "${MASK_4}" gw "10.134.1.4" dev "${TAP1}"
#	sudo route add -net "${BENTLEY}" netmask "${MASK_4}" gw "10.134.1.3" dev "${TAP1}"
#	sudo route add -net "${VOLKSWAGEN}" netmask "${MASK_5}" gw "10.134.1.3" dev "${TAP1}"
#	sudo route add -net "${SUZUKI}" netmask "${MASK_3}" gw "10.134.1.3" dev "${TAP1}"
#	sudo route add -net "${FERRARI}" netmask "${MASK_4}" gw "10.134.1.1" dev "${TAP1}"
#	sudo route add -net "${CADILLAC}" netmask "${MASK_6}" gw "10.134.1.1" dev "${TAP1}"
#	sudo route add -net "${MERCEDEZBENZ}" netmask "${MASK_6}" gw "10.134.1.3" dev "${TAP1}"
#	sudo route add -net "${FRAMERELAY}" netmask "${MASK_3}" gw "10.134.1.1" dev "${TAP1}"
#	sudo route add -net "${RENAULT}" netmask "${MASK_1}" gw "10.134.1.3" dev "${TAP1}"
#	sudo route add -net "${TUNEL}" netmask "${MASK_5}" gw "10.134.1.3" dev "${TAP1}"
#	sudo route add -net "${PEUGEOT}" netmask "${MASK_2}" gw "10.134.1.3" dev "${TAP1}"
	
	#route TAP2
#	sudo route add -net "${TOYOTA}" netmask "${MASK_1}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${AUDI}" netmask "${MASK_1}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${FORD}" netmask "${MASK_1}" gw "10.134.13.3" dev "${TAP2}"
#	sudo route add -net "${LAMBORGHINI}" netmask "${MASK_4}" gw "10.134.13.2" dev "${TAP2}"
#	sudo route add -net "${ROLLSROYCE}" netmask "${MASK_4}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${BENTLEY}" netmask "${MASK_4}" gw "10.134.13.3" dev "${TAP2}"
#	sudo route add -net "${VOLKSWAGEN}" netmask "${MASK_5}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${SUZUKI}" netmask "${MASK_3}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${FERRARI}" netmask "${MASK_4}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${CADILLAC}" netmask "${MASK_6}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${MERCEDEZBENZ}" netmask "${MASK_6}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${FRAMERELAY}" netmask "${MASK_3}" gw "10.134.13.1" dev "${TAP2}"
#	sudo route add -net "${RENAULT}" netmask "${MASK_1}" gw "10.134.13.3" dev "${TAP2}"
#	sudo route add -net "${TUNEL}" netmask "${MASK_5}" gw "10.134.13.3" dev "${TAP2}"
#	sudo route add -net "${FIAT}" netmask "${MASK_1}" gw "10.134.13.1" dev "${TAP2}"
	
	
	exit 0
else
	echo "El cliente debe ser: 'WEBSERVER', 'FTPSERVER', 'TELSERVER', 'HOSTA', 'HOSTB' O 'HOSTC'"
	exit 1
fi


exit 0

